﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;

namespace LCA.Community.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for MessagesRecipentsRepositoryTest and is intended
    ///to contain all MessagesRecipentsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MessagesRecipentsRepositoryTest : BaseTestClass 
    {
        private List<User> _users;
        private UsersFactory _usersFactory = new UsersFactory();
        private List<Message> _messages;
        private List<Message> _helpRequests;
        private MessagesFactory _messagesFactory = new MessagesFactory();
        private MessageRecipentsFactory _messageRecipentsFactory = new MessageRecipentsFactory();
        private List<MessageRecipent> _messageRecipents;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users = _usersFactory.CreateList(10);
            _users.ForEach(user => usersRepository.Add(user));

            
            var messagesRecipentsRepository = _unityContainer.Resolve<IMessagesRecipentsRepository>();
            var messagesRepository = new MessagesRepository(_unityContainer.Resolve <IDataServiceContext<Message>>(), messagesRecipentsRepository);
            _messages = new List<Message>();
            _helpRequests = new List<Message>();
            _messageRecipents = new List<MessageRecipent>();

            var recipents = new List<string> { _users[1].RowKey, _users[2].RowKey, _users[3].RowKey };
            var message = _messagesFactory.CreateEntity(_users[0].RowKey, recipents);
            _messages.Add(message);
            messagesRepository.Add(message);

            recipents = new List<string> { _users[1].RowKey, _users[4].RowKey, _users[5].RowKey };
            message = _messagesFactory.CreateEntity(_users[0].RowKey, recipents);
            _messages.Add(message);
            messagesRepository.Add(message);

            message = _messagesFactory.CreateHelpRequest(_users[0].RowKey, _users[6].RowKey);
            _helpRequests.Add(message);
            messagesRepository.Add(message);

            message = _messagesFactory.CreateHelpRequest(_users[1].RowKey, _users[7].RowKey);
            _helpRequests.Add(message);
            messagesRepository.Add(message);

            _messageRecipents = messagesRecipentsRepository.Entities;
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var messagesRepository = _unityContainer.Resolve<IMessagesRepository>();
            var messagesRecipentsRepository = _unityContainer.Resolve<IMessagesRecipentsRepository>();
            usersRepository.DeleteTestEntities();
            messagesRepository.DeleteTestEntities();
            messagesRecipentsRepository.DeleteTestEntities();
        }

        /// <summary>
        ///A test for DeleteAllUserMessageRecipents
        ///</summary>
        [TestMethod()]
        public void DeleteAllUserMessageRecipentsTest()
        {
            var target = _unityContainer.Resolve<IMessagesRecipentsRepository>();
            string userID = _users[1].RowKey;
            target.DeleteAllReceiverMessageRecipents(userID);

            var actual = target.GetMessageRecipentsByReceiver(userID);
            CollectionAssert.AreEquivalent(new List<MessageRecipent>(), actual);
        }

        /// <summary>
        ///A test for GetMessageRecipentsByUser
        ///</summary>
        [TestMethod()]
        public void GetMessageRecipentsByReceiverTest()
        {
            var target = _unityContainer.Resolve<IMessagesRecipentsRepository>();
            string userID = _users[1].RowKey;
            List<MessageRecipent> expected = _messageRecipents.Where(mr => mr.Recipent == userID).ToList();
            List<MessageRecipent> actual;

            actual = target.GetMessageRecipentsByReceiver(userID);
            CollectionAssert.AreEquivalent(expected, actual);
        }

        /// <summary>
        ///A test for GetMessageRecipentsByMessage
        ///</summary>
        [TestMethod()]
        public void GetMessageRecipentsByMessageTest()
        {
            var target = _unityContainer.Resolve<IMessagesRecipentsRepository>();
            string messageId = _messages[0].RowKey;

            List<MessageRecipent> expected = _messageRecipents.Where(mr => mr.Message == messageId).ToList();
            List<MessageRecipent> actual;

            actual = target.GetMessageRecipentsByMessage(messageId);
            CollectionAssert.AreEquivalent(expected, actual);
        }

        /// <summary>
        ///A test for DeleteAllMessageRecipentsFromMessage
        ///</summary>
        [TestMethod()]
        public void DeleteAllMessageRecipentsFromMessageTest()
        {
            var target = _unityContainer.Resolve<IMessagesRecipentsRepository>();
            string messageId = _messages[0].RowKey;

            List<MessageRecipent> expected = new List<MessageRecipent>();
            List<MessageRecipent> actual;

            target.DeleteAllMessageRecipentsFromMessage(messageId);

            actual = target.GetMessageRecipentsByMessage(messageId);
            CollectionAssert.AreEquivalent(expected, actual);
        }
    }
}
